package cn.crm.service.impl;

import cn.crm.mapper.CrmStaffMapper;
import cn.crm.pojo.CrmStaff;
import cn.crm.pojo.CrmStaffExample;
import cn.crm.service.StaffService;
import cn.crm.utils.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private CrmStaffMapper crmStaffMapper;

    @Override
    public CrmStaff findUserAndPass(String loginName, String passWord) {
        //根据用户名进行查询   条件查询
        CrmStaffExample example=new CrmStaffExample();
        CrmStaffExample.Criteria criteria = example.createCriteria();
        //Criteria用来构建查询条件
        criteria.andLoginnameEqualTo(loginName); // where loginName=""

        List<CrmStaff> crmStaffs = crmStaffMapper.selectByExample(example);

        //判断是否查询来用户
        CrmStaff crmStaff=null;
        if(crmStaffs!=null&&crmStaffs.size()>0){
              crmStaff=crmStaffs.get(0);
              //判断密码是否正确
             if(!crmStaff.getLoginpwd().equals(DigestUtils.md5DigestAsHex(passWord.getBytes()))){
                 return null;
             }else
                 return crmStaff;
        }
        return crmStaff;
    }

    //分页展示所有员工信息
    @Override
    public PageUtil findStaffInfo(int page, int size) {
        PageHelper.startPage(page,size);
        //查询所有员工的功能
        List<CrmStaff> staffInfos = crmStaffMapper.findStaffInfo();

        PageUtil pageUtil=new PageUtil();
        pageUtil.setDatas(staffInfos);

        PageInfo<CrmStaff> pageInfo=new PageInfo<>(staffInfos);
        long total = pageInfo.getTotal();//得到总记录数

        long totalPage =total/size;
        if(total%size!=0)
            totalPage++;

        pageUtil.setCurrentPage(page);
        pageUtil.setTotalPage(totalPage);
        return pageUtil;
    }

    //条件查询员工信息
    @Override
    public PageUtil findStaffByCondition(int page, int size, CrmStaff crmStaff) {
        PageHelper.startPage(page,size);

        //查询所有符合条件的记录
        List<CrmStaff> staffs = crmStaffMapper.findStaffInfoByCondition(crmStaff);

        PageUtil pageUtil=new PageUtil();
        pageUtil.setDatas(staffs);

        PageInfo<CrmStaff> pageInfo=new PageInfo<>(staffs);
        long total=pageInfo.getTotal();

        long totalPage=total/size;
        if(total%size!=0)
            totalPage++;

        pageUtil.setCurrentPage(page);
        pageUtil.setTotalPage(totalPage);
        return pageUtil;
    }

    //根据员工id查询员工信息
    @Override
    public CrmStaff findStaffById(String staffId) {
        //调用mapper
        CrmStaff staff = crmStaffMapper.findStaffById(staffId);

        return staff;
    }

    //修改员工信息
    @Override
    public void updateStaffInfo(CrmStaff crmStaff) {

        crmStaffMapper.updateByPrimaryKey(crmStaff);
    }

    //添加员工
    @Override
    public void addStaff(CrmStaff crmStaff) {
        //补全页面上没有的数据
        crmStaff.setStaffid(UUID.randomUUID().toString());
        String pass=DigestUtils.md5DigestAsHex(crmStaff.getLoginpwd().getBytes());
        crmStaff.setLoginpwd(pass);

        crmStaffMapper.insert(crmStaff);

    }
}
